package com.secsys.action;

import java.io.PrintWriter;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.secsys.dto.FriendsDTO;
import com.secsys.model.TbSysFriends;
import com.secsys.service.FriendsService;
import com.secsys.util.Constant;

/**
 * @Description 好友信息控制器
 * @author LiusQ
 * 
 */
public class FriendsAction extends BaseAction<FriendsDTO> {
	private FriendsDTO friendsDTO;
	private FriendsService friendsService;

	public void setFriendsService(FriendsService friendsService) {
		this.friendsService = friendsService;
	}

	public FriendsDTO getFriendsDTO() {
		return friendsDTO;
	}

	public void setFriendsDTO(FriendsDTO friendsDTO) {
		this.friendsDTO = friendsDTO;
	}

	/**
	 * 
	 * @Description 添加好友
	 * @return
	 * @throws Exception
	 */
	public String addFriend() throws Exception {
		String resultCode=LOGIN;
		String msg=null;
		String url=null;
		String titleURL=null;
		if (friendsDTO.getUserId() != null && friendsDTO.getFriendId() != null
				&& !friendsDTO.getUserId().equals(friendsDTO.getFriendId())) {   //不能将自己添加为好友
			     TbSysFriends friends=friendsService.queryFriendsByFriendId(Integer.parseInt(friendsDTO.getUserId()),Integer.parseInt(friendsDTO.getFriendId()));
			     if(friends==null){   //假如好友不存在,执行添加好友操作
			          friendsService.addFriends(Integer.parseInt(friendsDTO.getUserId()),
					Integer.parseInt(friendsDTO.getFriendId()));
			          msg="添加好友成功！";
			          url="<a href='" + Constant.DEFAULT_CONTROLPAGE
						+ "'>返回查看好友列表" + "</a>";
			          titleURL=Constant.SHOW_FRIEND_BY_ID + "?userId="
						+ friendsDTO.getUserId();
			     }else{
			    	 msg="添加错误,该好友已经存在...";
			    	 url="<a href='" + Constant.DEFAULT_CONTROLPAGE
						+ "'>返回查看好友列表" + "</a>";
			    	 titleURL=Constant.SHOW_FRIEND_BY_ID + "?userId="
						+ friendsDTO.getUserId();
			     }
		}else{   //假如添加的是自己,将跳转提示"SORRY,YOU DON'T ADD YOUSELF AS A FRIENDS";
			 msg="添加错误,不能将自己添加为好友！";
			 url="<a href='" + Constant.DEFAULT_CONTROLPAGE
				+ "'>返回查看好友列表" + "</a>";
			 titleURL=Constant.SHOW_FRIEND_BY_ID + "?userId="
				+ friendsDTO.getUserId();
		}
		session.put("msg",msg );

		session.put("url",url);
		session.put("titleURL", titleURL );
		return "msgTip";
	}

	/**
	 * 
	 * @Description 通过ID查询好友
	 * @return
	 * @throws Exception
	 */
	public String queryFriendById() throws Exception {
		System.out.println("查询好友列表");
		System.out.println(friendsDTO.getUserId());
		List friendList = friendsService.queryFriendsById(Integer.parseInt(friendsDTO.getUserId()));
	 
		session.put("friendList", friendList);
		return "queryFriendList";
	}

	/**
	 * 
	 * @Description 删除好友关系
	 * @return
	 * @throws Exception
	 */
	public String delFriends() throws Exception {

		String id = ServletActionContext.getRequest().getParameter("id");
		if (id != null && id != "") {
			friendsService.delFriend(Integer.parseInt(id));
		} else {
			return "msgTip";
		}
		return "queryFriendById";
	}
   
	/**
	 * 
	 * @Description  ajax方式获取好友列表
	 * @return
	 * @throws Exception
	 */
	public String ajaxGetFriendInfo()throws Exception{
		System.out.println("请求成功");
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
	   PrintWriter out=ServletActionContext.getResponse().getWriter(); 
	    List friendList=friendsService.queryFriendsById(Integer.parseInt(friendsDTO.getUserId())); //获取好友列表
		StringBuffer friendMsg=null;
		if(friendList!=null&&friendList.size()>0){
			friendMsg=new StringBuffer("<select name='friendId' id=\"friendsId\" onchange='addReceiver(this)'><option value=' '>--请选择--</option>");
	    for(int i=0;i<friendList.size();i++){
	    	TbSysFriends friends=(TbSysFriends)friendList.get(i);
			   friendMsg.append("<option name='"+friends.getTbSysUserByFriendsId().getUserName()+"' value='"+friends.getTbSysUserByFriendsId().getId()+"'>"+friends.getTbSysUserByFriendsId().getUserName()+"</option>");
		      
	    }
	    friendMsg.append("</select>");
		}else{
		    friendMsg=new StringBuffer("暂无好友");	
		}
		out.print(friendMsg.toString()); 
		return null;
	}
	
	
	@Override
	public FriendsDTO getModel() {
		// TODO Auto-generated method stub
		if (friendsDTO == null) {
			friendsDTO = new FriendsDTO();
		}
		return friendsDTO;
	}

}
